<?php
/***************************************************************
*  Copyright notice
*  (c) 2009, jianyuzhu@gmail.com
*  All rights reserved
*  This script is part of the PPEMI project.
***************************************************************/

//
function func_lang_translation($params, $content, &$smarty, &$repeat) {
	global $lang;
	global $LANGUAGE;
	
	if( isset($content) ) {
		//$p_lang = $params['lang'];
		$p_module = $params['module'];
		$p_class = $params['class'];
		
		//
		if( $p_module != '' ) {
			func_lang_dynamic_load_package($p_module);
		}
		if( $p_module && $p_class && isset($LANGUAGE[$p_module][$p_class][$content]) ) {
			$translation = $LANGUAGE[$p_module][$p_class][$content];
		} elseif( $p_module && isset($LANGUAGE[$p_module][$content]) ) {
			$translation = $LANGUAGE[$p_module][$content];
		} elseif( isset($LANGUAGE[$content]) ) {
			$translation = $LANGUAGE[$content];
		} else {
			$translation = $content;
		}
		
		return $translation;
	}
}

//
function func_lang_get($key, $module = '', $load = false) {
	if( $module != '' ) {
		func_lang_dynamic_load_package($module);
	}
	global $LANGUAGE;
	if( strstr($key, ':') != false ) {
		if( substr($key, 0, 1) == ':' ) {
			$key = substr($key, 1);
		}
		$a = explode(':', $key);
		$key = $a['1'];
		$module = $a['0'];
		
	} elseif( $module == '' ) {
		return $key;
	}
	if( isset($LANGUAGE[$module][$key]) ) {
		return $LANGUAGE[$module][$key];
	} else {
		$key = ucfirst(str_replace('_', ' ', $key));
	}
	
	return $key;
}

//
function _lang($key, $module = '', $load = false) {
	return func_lang_get($key, $module, $load);
}

//
function func_lang_request_language() {
	global $request_type;
	
	$request_language = $request_type;
	if( $request_type == 'gb' ) {
		$request_language = 'guestbook';
	} elseif( $request_type == '' || $request_type == 'index' || $request_type == 'home' ) {
		$request_language = 'home';
	} elseif( $request_type == 'c' || $request_type == 'a' ) {
		$request_language = 'content';
	}
	
	return $request_language;
}

function func_lang_load_package($files = '', $type = '_listfile_') {
	global $lang;
	
	if( $lang == 'en_us' || $lang == 'en' ) {
		$lang_package = 'en_us';
	} elseif( $lang == 'zh_tw' || $lang == 'tw' ) {
		$lang_package = 'zh_tw';
	} elseif( $lang == 'zh_cn' || $lang == 'cn' ) {
		$lang_package = 'zh_cn';
	} else {
		$lang_package = 'en_us';
	}
	if( $type != '_listfile_' ) {
		$files = func_get_args();
	}
	if( is_array($files) ) {
		for($i=0, $n=sizeof($files); $i<$n; $i++) {
			if( strlen($files[$i]) > 0 && file_exists(CONFIG_PATH . CONFIG_DIR_LANGUAGES . $lang_package . '/' . $files[$i] . '.language.php') ) {
				include_once(CONFIG_PATH . CONFIG_DIR_LANGUAGES . $lang_package . '/' . $files[$i] . '.language.php');
			}
		}
	} else {
		if( strlen($files) > 0 && file_exists(CONFIG_PATH . CONFIG_DIR_LANGUAGES . $lang_package . '/' . $files . '.language.php') ) {
			include_once(CONFIG_PATH . CONFIG_DIR_LANGUAGES . $lang_package . '/' . $files . '.language.php');
		}
	}
}

function func_lang_dynamic_load_package($files = '', $type = '_listfile_') {
	global $lang;
	$request_language = func_lang_request_language();
	
	if( $lang == 'en_us' || $lang == 'en' ) {
		$lang_package = 'en_us';
	} elseif( $lang == 'zh_tw' || $lang == 'tw' ) {
		$lang_package = 'zh_tw';
	} elseif( $lang == 'zh_cn' || $lang == 'cn' ) {
		$lang_package = 'zh_cn';
	} else {
		$lang_package = 'en_us';
	}
	if( $type != '_listfile_' ) {
		$files = func_get_args();
	}
	//
	include_once(CONFIG_PATH . CONFIG_DIR_LANGUAGES . $lang_package . '/global.language.php');
	if( is_array($files) ) {
		for($i=0, $n=sizeof($files); $i<$n; $i++) {
			if( strlen($files[$i]) > 0 && file_exists(CONFIG_PATH . CONFIG_DIR_LANGUAGES . $lang_package . '/' . $files[$i] . '.language.php') ) {
				include_once(CONFIG_PATH . CONFIG_DIR_LANGUAGES . $lang_package . '/' . $files[$i] . '.language.php');
			}
		}
	} else {
		if( strlen($files) > 0 && file_exists(CONFIG_PATH . CONFIG_DIR_LANGUAGES . $lang_package . '/' . $files . '.language.php') ) {
			include_once(CONFIG_PATH . CONFIG_DIR_LANGUAGES . $lang_package . '/' . $files . '.language.php');
		}
	}
	//
	if( strlen($request_language) > 0 && file_exists(CONFIG_PATH . CONFIG_DIR_LANGUAGES . $lang_package . '/' . $request_language . '.language.php') ) {
		include_once(CONFIG_PATH . CONFIG_DIR_LANGUAGES . $lang_package . '/' . $request_language . '.language.php');
	}
}
//
?>